package com.my.loginbmob.presenter;

import android.util.Log;

import com.my.loginbmob.bean.UserBmobBean;
import com.my.loginbmob.check_util.CheckUser;
import com.my.loginbmob.view.iview.ILoginView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * 项目名称：登录控制层
 * 创建人：mwb
 * 创建时间：2018/8/2 16:12
 */

public class LoginPresenter implements ILoginPresenter, ICheckUserName {
    private String TAG = "mwbmvp";
    private ILoginView iloginView;
    private CheckUser checkUser;
    private String userName;
    private String password;

    public LoginPresenter(ILoginView iLoginView) {
        this.iloginView = iLoginView;
        checkUser = new CheckUser(this);
    }

    @Override
    public void goLogin(String userName, String password) {
        checkInpt(userName, password);
    }

    @Override
    public void checkInpt(String userName, String password) {
        if (userName.isEmpty()) { // 没有输入用户名
            iloginView.setProgressVisibility(0);
            iloginView.setErroToast("请输入用户名");
        } else if (userName.length() < 6) {
            iloginView.setProgressVisibility(0);
            iloginView.setErroToast("用户名为6-16位");
        } else if (password.isEmpty()) {
            iloginView.setProgressVisibility(0);
            iloginView.setErroToast("请输入密码");
        } else if (password.length() < 6) {
            iloginView.setProgressVisibility(0);
            iloginView.setErroToast("密码为6-16位");
        } else {
            loginForServer(userName, password);
        }
    }

    /**
     * 登录
     *
     * @param userName
     * @param password
     */
    @Override
    public void loginForServer(String userName, String password) {
        this.userName = userName;
        this.password = password;
        checkUser.checkUserName(userName);
    }

    @Override
    public void checkUserName(int num) {
        if (num == -1) {
            iloginView.setProgressVisibility(0);
            iloginView.loginResule(false, "登录失败");
        } else if (num > 0) { // 有此用户
            checkUserNameAndPassword();
        } else {
            iloginView.setProgressVisibility(0);
            iloginView.loginResule(false, "无此用户");
        }
    }

    /**
     * 检查用户名和密码
     */
    private void checkUserNameAndPassword() {
        BmobQuery<UserBmobBean> query = new BmobQuery<UserBmobBean>();
        query.addWhereEqualTo("username", userName);

        BmobQuery<UserBmobBean> query2 = new BmobQuery<UserBmobBean>();
        query2.addWhereEqualTo("password", password);

        List<BmobQuery<UserBmobBean>> andQuerys = new ArrayList<BmobQuery<UserBmobBean>>();
        andQuerys.add(query);
        andQuerys.add(query2);

        BmobQuery<UserBmobBean> query3 = new BmobQuery<UserBmobBean>();
        query3.and(andQuerys);
        query3.findObjects(new FindListener<UserBmobBean>() {
            @Override
            public void done(List<UserBmobBean> list, BmobException e) {
                if (e == null) {
                    Log.i(TAG, "登录查询成功:" + list.size());

                    if (list.size() > 0) {
                        iloginView.setProgressVisibility(0);
                        iloginView.loginResule(true, "登录成功");
                    } else {
                        iloginView.setProgressVisibility(0);
                        iloginView.loginResule(false, "用户名或密码错误");
                    }
                } else {
                    Log.i(TAG, "登录查询失败:" + list.size());
                    iloginView.setProgressVisibility(0);
                    iloginView.loginResule(false, "登录失败");
                }
            }
        });

    }
}
